//
//  test.m
//  go2
//
//  Created by 袁飞 on 12-7-21.
//
//

#import "test.h"
#import "FObject.h"
#import "FUtil.h"
#import "FListView.h"
#import "ButtonHandle.h"
#import "FProgressBar.h"
#import "FSwitch.h"

@implementation test
-(id)init{
  [super init];
  srand((unsigned int)time(NULL));
  
  [CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
  [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB565];

  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"UIShop.plist"];
  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"UICommon.plist"];
  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"UIProgressBar.plist"];
  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"UIDialogGolden1.plist"];

  
  //[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Icon101156.plist"];
  
  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"201012.plist"];
  
  [[ButtonHandle sharedButtonHandle] setLayer:self];
  
  NSString* fullfilename = [[CCFileUtils sharedFileUtils] fullPathFromRelativePath:@"normals.txt"];
  NSMutableArray *ar = [NSMutableArray arrayWithContentsOfFile:fullfilename];
  NSString *data = [NSString stringWithContentsOfFile:fullfilename encoding:NSUTF8StringEncoding error:nil];
  ar = [FUtil splitStr:data byFlag:@"\n"];
  for (NSString* o in ar) {
    //NSLog(@"-------%@", o);
    NSString *tmp = [NSString stringWithFormat:@"%@_normal.plist", o];
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:tmp];
    tmp = [NSString stringWithFormat:@"Icon%@.plist", o];
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:tmp];
  }

  
  CCSprite* sp = [CCSprite spriteWithSpriteFrameName:@"201012.png"];
  [sp setPosition:ccp(240, 160)];
  [self addChild:sp];
  
 
  /*
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  [dict setObject:@"101011" forKey:@"fishno"];
  o_ = [FObject FObjectFromXmlFile:@"obj.xml" WithDict:nil];
  [self addChild:o_];
  FAI *ai = [[FSeqAI alloc] initWithFObject:o_];
  [o_ setAi:ai];
  [o_ startAi];*/
  
  
  CCLayer * cl = [[CCLayer alloc] init];
  int begin = 2500;
  for (int i=0; i<100; i++) {
    NSString* tmp = [ar objectAtIndex:i];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:tmp forKey:@"fishno"];
    CCLayer* tl = [FUtil layerFromFile:@"fishpanel.xml" withDict:dict];
    [cl addChild:tl];
    [tl setPosition:ccp(0, begin-i*50-30)];
  }
  //[self addChild:cl];
  //CCLayer* l2 = [FUtil layerFromFile:@"cl2.xml" withDict:nil];
  //[self addChild:l2];
  [cl setContentSize:CGSizeMake(100, 5020)];
  
  list_ = [[FListView alloc] initWithContent:cl];
  [list_ setContentSize:CGSizeMake(100, 320)];
  [list_ setPosition:ccp(0, 0)];
  [self addChild:list_];
  [list_ retain];
  
  
  FButton *b = [FButton FButtonWithName:@"showwnd" Normal:@"btn_4_clicked.png" Highlight:@"btn_4.png" Disabled:@"btn_4_clicked.png" Scaled:1];
  [self addChild:b];
  [b setPosition:ccp(400, 300)];
  
  FSwitch *sw = [[FSwitch alloc] initWithOn:@"btn_4.png" Off:@"btn_4_clicked.png"];
  [self addChild:sw];
  [sw setPosition:ccp(250, 300)];

  //[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
  /*CCSprite* sp1 = [CCSprite spriteWithSpriteFrameName:@"bar_1.png"];
  [sp1 setPosition:ccp(100,200)];
  [self addChild:sp1];
  
  CCSprite* sp2 = [CCSprite spriteWithSpriteFrameName:@"bar_bg_1.png"];
  [sp2 setPosition:ccp(100,150)];
  [self addChild:sp2];

  
  FProgressBar* bar = [[FProgressBar alloc] initWithBK:@"bar_bg_1.png" Bar:@"bar_1.png" Total:100];
  [self addChild:bar];
  [bar setPosition:ccp(100, 100)];*/
  return self;
}

-(void)change:(NSString*)name{
  //[self removeChild:o_ cleanup:false];
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  [dict setObject:name forKey:@"fishno"];
  FObject* o = [FObject FObjectFromXmlFile:@"obj.xml" WithDict:dict];
  [self addChild:o];
  //[o_ setPosition:ccp(100,50)];
  //[o2 runActionRepeated:@"eat" WithSprite:@"test"];
  FRandomAI *ai = [[FRandomAI alloc] initWithFObject:o];
  [o setAi:ai]; 
  [o startAi];
  
  [[ButtonHandle sharedButtonHandle] addObject:o];
}

-(void)showList:(bool)show{
  if (show) {
    [self addChild:list_];
  }else{
    [self removeChild:list_ cleanup:false];
  }
}

@end
